package com.iwhalecloud.bss.kite.cucc.service.attr.handler;


import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.data.CustAttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName : LocalProvinceAttrHandler
 * @Description : 装机地址省份
 * @Author : xie.zhengjun
 * @Date : 2020/9/22 16:46
 */
public class LocalProvinceAttrHandler extends AbstractAttrHandler<KiteProdInst, CustAttrData> {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Resource
    private OperateInstAttrService operateInstAttrService;

    @Override
    public void init(KiteAttr attr, KiteProdInst inst) {
        super.init(attr, inst);
        LocalAttrHelper.initPlatformProvince(attr, inst.getProdId());
    }

    @Override
    public void after(KiteAttr attr, KiteProdInst inst, CustAttrData data) {
        // 【上海光纤】质检页面和签订页面的商品信息编辑中 装机地址相关信息是不可修改的
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if (KiteStringUtils.equalsAny(data.getServiceOfferId(), LocalServiceOfferConsts.SERVICE_OFFER_ORDER_SIGN,
            LocalServiceOfferConsts.SERVICE_OFFER_QC, LocalServiceOfferConsts.SERVICE_OFFER_FOLLOW) || (Objects.nonNull(extParams) && extParams.containsKey("nodeCode"))) {
            attr.setIsEdit(KeyConsts.IFTRUE_F);
            attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_M);
        }
        if (inst.getActionType().equals(ActionType.A)) {
            String postProvinceNbr = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
            operateInstAttrService.setAttrValue(inst, attr.getFieldName(), postProvinceNbr, data.getSceneInstId(), null);
        }
        super.after(attr, inst, data);
    }
}
